package com.geek688.auxiliarytool.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.commons.lang3.StringUtils;

import com.geek688.auxiliarytool.bean.Log;
import com.geek688.auxiliarytool.util.DBUtil;
import com.geek688.auxiliarytool.util.LogUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DBUI extends JFrame implements ActionListener{
	
	private double WIDTH = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
	private double HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
	
	private static final long serialVersionUID = 1L;
	private JTextField urlField,portField,dbnameField,usernameField;
	private JPasswordField passwordField;
	//数据库配置
	private String URL = "jdbc:mysql://##?useUnicode=true&amp;characterEncoding=utf-8";//5.1
	private String USER = "##";
	private String PASSWORD = "##";
    private DataSource dataSource = null;
    private DBUtil db = null;
	private boolean enableFlag;
	private static DBUI dbui = null;
    
	/**
	 * 绘制界面
	 * @author Jack
	 */
	private DBUI() {
		this.setLocation(new Point((int) (WIDTH / 2) -175, (int) (HEIGHT / 2)-150));
//		setLocationRelativeTo(null);
        this.setSize(350, 300);  
        this.setTitle("设置"); 
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
		//读取日志文件
        Log readLog = LogUtil.readLog();
		JPanel sqlPanel = new JPanel(new GridLayout(6, 2, 0, 5));
        sqlPanel.setPreferredSize(new Dimension(250, 175));
//        sqlPanel.setBorder(BorderFactory.createLineBorder(Color.gray,1));//边框
        JLabel url = new JLabel("数据库地址：");
        urlField = new JTextField(readLog.getUrl(),30);
        JLabel port = new JLabel("端    口：");
        portField = new JTextField(readLog.getPort());
        JLabel dbname = new JLabel("数据库名：");
        dbnameField = new JTextField(readLog.getDbname());
        JLabel username = new JLabel("用户名：");
        usernameField = new JTextField(readLog.getUsername());
        JLabel password = new JLabel("密    码：");
        passwordField = new JPasswordField(readLog.getPassword());
        JButton setBtn = new JButton("检测连接");
        setBtn.setActionCommand("set");setBtn.addActionListener(this);
        sqlPanel.add(url);sqlPanel.add(urlField);
        sqlPanel.add(port);sqlPanel.add(portField);
        sqlPanel.add(dbname);sqlPanel.add(dbnameField);
        sqlPanel.add(username);sqlPanel.add(usernameField);
        sqlPanel.add(password);sqlPanel.add(passwordField);
        this.add(sqlPanel);
        this.add(setBtn);
        this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("set")) {
			String url = urlField.getText().trim().replaceAll("(：)|(:)", ":");
			String port = portField.getText().trim();
			String dbname = dbnameField.getText().trim();
			String username = usernameField.getText().trim();
			String password = new String(passwordField.getPassword());
			if (StringUtils.isBlank(url) || StringUtils.isBlank(port) || 
				StringUtils.isBlank(dbname) || StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
				JOptionPane.showMessageDialog(this,"请完善配置信息！");
				return;
			}
			Log readlog = new Log();
			readlog.setDbname(dbname);
			readlog.setUsername(username);
			readlog.setPassword(password);
			readlog.setUrl(url);
			readlog.setPort(port);
			LogUtil.writeLog(readlog);
			String jdbcUrl = URL.replaceAll("##", url+":"+port+"/"+dbname);
			username = USER.replaceAll("##", username);
			password = PASSWORD.replaceAll("##", password);
			db = DBUtil.getInstance();
			db.setParams(jdbcUrl, username, password);
			new Thread(()->{
				dataSource = DBUtil.getSource();
				Connection conn = null;
				try {
					conn = dataSource.getConnection();
					if (conn != null) {
						enableFlag = true;
						JOptionPane.showMessageDialog(this,"测试连接成功");
					}
				} catch (SQLException e1) {
					enableFlag = false;
					DBUtil.setSource(null);
					JOptionPane.showMessageDialog(this,"测试连接失败");
					log.error("",e1);
				}
			}).start();
		}
	}

	public boolean isEnableFlag() {
		return enableFlag;
	}
	
	public static DBUI getInstance() {
		if (dbui == null) {
			dbui = new DBUI();
		}
		dbui.setVisible();
		return dbui;
	}
	public void setVisible() {
		this.setVisible(true);
	}
}
